<%@ include file="/common.jsp"%>  
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
		<title>酱出名门人事工资管理平台</title>

		<link type="text/css"
			href="${ctx}/js/dhtmlxTree/dhtmlxtree.css" rel="stylesheet" />
		<script type="text/javascript" src="${ctx}/js/dhtmlxTree/dhtmlxcommon.js"></script>
		<script type="text/javascript" src="${ctx}/js/dhtmlxTree/dhtmlxtree.js"></script>

	</head>

	<BODY>
		<div class="main">
			<%@include file="/head.jsp"%>
			<div class="nav">
				<ul>
					<li>
						<a href="roleAction.action">权限管理</a>
					</li>
					<li class="nav_pic"></li>	
     				<li>
     					<a href="roleAction!jumpToAdd.action">添加角色</a>
     				</li>			
					<li class="nav_pic"></li>

				</ul>
			</div>
	 		<div class="left" style="height:400px;">
	 		  <ul>
	 		  <li style="background-color:#5e7291;color:#fff;text-align:center;font-size:14px;font-weight:800;">功能列表</li>
	 		  </ul>
			  <div id="fm_xmltree" ></div>
	       </div>			
	
	 		
	 <div class="right" style="height:400px;">
		<form class="add_role" >	 
		<label for="dengluming">角色名称：</label>
		<input type="text" id="edit_role_name" class="text ui-widget-content ui-corner-all"  style="height:30px;line-height:30px;"><br />
		<label for="mima">备注：</label>
		<textarea rows="5" id="edit_role_remark" class="text ui-widget-content ui-corner-all ui-widget"  style="float: left;margin-top: 25px;line-height:30px;width:200px"></textarea><br /><%--
        <input type="text"   name="" class="text ui-widget-content ui-corner-all"  style="height:30px;line-height:30px;"><br />--%>
		<input type="submit" value="确定" style="width:60px;margin-left:300px;">
		<input type="reset" style="width:60px;margin-left:10px;">
	    </form>
	 
	 </div>

			<div class="clear"></div>
			<div class="footer">
				酱出名门人事工资管理平台 版权所有
			</div>
		</div>
	</body>
	<script type="text/javascript" >
	    //加载树形菜单 
		tree = new dhtmlXTreeObject("fm_xmltree", "100%", "350px", 0);
		tree.setSkin('dhx_skyblue');
		tree.setImagePath("${ctx}/js/dhtmlxTree/imgs/csh_bluebooks/");
		tree.enableCheckBoxes(1);
		tree.enableThreeStateCheckboxes(false);
		tree.loadXML("getXmlTreeFm.action");

        
		$(function() {
			//提交添加角色操作
			$(":submit").click(function () {
				var checkedPItems = getCheckedPermission();

			    
				if (checkedPItems == null || checkedPItems.length < 1)  {
					alert("请选择模块操作权限!");
					return false;
				} 

                var role_name =  $.trim($("#edit_role_name").val());
                var role_remark = $("#edit_role_remark").val();
				if (role_name == "") {
                	alert("角色名不能为空!");
                	return false;
                }
                
				var permissionIdsJson = JSON.stringify(checkedPItems);
				var roleObjRef = new roleObj(role_name, role_remark);
				var roleJson = JSON.stringify(roleObjRef);
				addRole(roleJson, permissionIdsJson);
								
				return false;
				
			}); 

			$( "input:submit, input:reset").button();
		});

		//添加角色
        function addRole(roleJson, permissionIdsJson) {
        	$.ajax({
        		   type: "POST",
        		   url: "roleAction!addRole.action",
        		   data: "roleJson=" + roleJson + "&permissionIdsJson=" + permissionIdsJson + "&timestamp=" + new Date().getTime(),
        		   success: function(msg){
        		       addRoleCallback(msg);
        		   },
        		   error:function (textStatus) {
            		  alert(textStatus); 
            	   }
            })
        }
        
		function addRoleCallback(msg) {
	        switch (msg.status.code) {
	        case "SUCCES" :
	        	alert(msg.status.message);
	        	window.location.href="roleAction.action"
	            break;
	        case "FAIL" :
	        	alert(msg.status.message);
	            break; 
	        }
	    }

		//获得树形菜单已选择的权限
        function getCheckedPermission() {
            var checkedItems = tree.getAllCheckedBranches().split(",");
            var checkedPItems = [];
			var j = 0;
            for (var i = 0; i < checkedItems.length; i++) {
                
				if (checkedItems[i].substring(0, 2) == "p_") {

					checkedPItems[j++] = checkedItems[i].substring(2, checkedItems[i].length); 
				}
            }
            return checkedPItems;
        }

		function roleObj(name, remark) {
			this.name = name;
			this.remark = remark;
		}
		
	</script>	
</html>
